<template>
  <div class="xtx-numbox">
    <!-- 某些场合不显示 -->
    <div class="label" v-if="isLabel">数量</div>
    <div class="numbox">
      <a href="javascript:;" @click="ins">-</a>
      <input type="text" readonly :value="count">
      <a href="javascript:;" @click="des">+</a>
    </div>
  </div>
</template>
<script>
// 1. 完成静态结构的搭建 + 最基础的增减逻辑  [增减点击的时候控制一下value属性]
// 2. 通过props控制最大最小值
// 3. 优化为v-model的实现
import { ref, watch } from 'vue'
export default {
  name: 'XtxNumber',
  props: {
    modelValue: {
      type: Number,
      default: 1
    },
    min: {
      type: Number,
      default: 1
    },
    max: {
      type: Number,
      default: 10
    },
    isLabel: {
      type: Boolean,
      default: true // 有限满足之前的写法 不能因为多一个参数之后破坏之前的写法
    }
  },
  setup (props, { emit }) {
    const count = ref(props.modelValue)
    function ins () {
      if (count.value === props.min) return
      count.value--
      emit('update:modelValue', count.value)
    }
    function des () {
      if (count.value === props.max) return
      count.value++
      emit('update:modelValue', count.value)
    }
    watch(() => props.modelValue, () => {
      count.value = props.modelValue
    })
    return {
      count,
      ins,
      des
    }
  }
}
</script>
<style scoped lang="less">
.xtx-numbox {
  display: flex;
  align-items: center;
  .label {
    width: 60px;
    color: #999;
    padding-left: 10px;
  }
  .numbox {
    width: 120px;
    height: 30px;
    border: 1px solid #e4e4e4;
    display: flex;
    > a {
      width: 29px;
      line-height: 28px;
      text-align: center;
      background: #f8f8f8;
      font-size: 16px;
      color: #666;
      &:first-of-type {
        border-right:1px solid #e4e4e4;
      }
      &:last-of-type {
        border-left:1px solid #e4e4e4;
      }
    }
    > input {
      width: 60px;
      padding: 0 5px;
      text-align: center;
      color: #666;
    }
  }
}
</style>
